package com.example.shuiyin.processors;

import com.example.shuiyin.enums.MediaType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;

@Component
@Slf4j
public class VideoProcessor implements MediaProcessor {

    @Value("${storage.local.path}")
    private String storageBasePath;

    @Override
    public String process(String sourceFilePath) {
        try {
            // 创建输出目录
            Path outputDir = Paths.get(storageBasePath, "results");
            Files.createDirectories(outputDir);
            
            // 生成输出文件名
            String outputFilename = UUID.randomUUID() + ".mp4";
            Path outputPath = outputDir.resolve(outputFilename);
            
            // 调用FFmpeg处理视频
            String command = String.format(
                    "ffmpeg -i \"%s\" -vf \"delogo=x=10:y=10:w=200:h=50:show=0\" -c:a copy \"%s\"",
                    sourceFilePath,
                    outputPath
            );
            
            log.info("执行命令: {}", command);
            
            Process process = Runtime.getRuntime().exec(command);
            int exitCode = process.waitFor();
            
            if (exitCode != 0) {
                throw new RuntimeException("视频处理失败，返回码: " + exitCode);
            }
            
            return outputPath.toString();
        } catch (Exception e) {
            log.error("视频处理失败: {}", e.getMessage(), e);
            throw new RuntimeException("视频处理失败: " + e.getMessage());
        }
    }

    @Override
    public MediaType supportedMediaType() {
        return MediaType.VIDEO;
    }
} 